package phototagger.imgsim;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.Color;
import java.io.File;
import java.io.IOException;

/**
 * This class uses a very simple, naive similarity algorithm to compare an image
 * with all others in the same directory.
 */
public class NaiveSimilarityFinder extends SimilarityFinder {
	
  public NaiveSimilarityFinder(File reference) throws IOException {
		super(reference);
  }
	
  public static double imageDistance ( Image i1, Image i2 ) {
	  BufferedImage img1 = toBufferedImage(i1);
	  BufferedImage img2 = toBufferedImage(i2);
	  Color[][] signature = calcSignature(rescale(img1));
	  Color[][] sigOther = calcSignature(rescale(img2));
	    double dist = 0;
	    for (int x = 0; x < 5; x++)
	      for (int y = 0; y < 5; y++)
	        {
	        int r1 = signature[x][y].getRed();
	        int g1 = signature[x][y].getGreen();
	        int b1 = signature[x][y].getBlue();
	        int r2 = sigOther[x][y].getRed();
	        int g2 = sigOther[x][y].getGreen();
	        int b2 = sigOther[x][y].getBlue();
	        double tempDist = Math.sqrt((r1 - r2) * (r1 - r2) + (g1 - g2) * (g1 - g2) + (b1 - b2) * (b1 - b2));
	        dist += tempDist;
	        }
	    return dist;
  }

}
